import json
import plotly as plotly
import plotly.graph_objects as go
with open('Sample LRS rinnuja.json') as json_file:
data = json.load(json_file)
print("data tyoe of json statements below: \n" + str(type(data)))
print("length of data: \n " + str(len(data)))
print("statement at index 0: \n " + str(data[0]))
print("acessing key and result in statement at index 0: \n " + str(data[0]['object']['id']))
data tyoe of json statements below:
<class 'list'>
length of data:
21343
statement at index 0:
{'actor': {'name': 'canvas test name', 'mbox': 'mailto:canvastestnamehere@gmail.com'}, 'verb': {'id': 'http://adlnet.gov/expapi/verbs/initialized', 'display': {'en-US': 'initialized'}}, 'object': {'id': 'https://elearn.ucr.edu/courses/3730', 'definition': {'name': {'en-US': 'Cal Labs Module 1: Method of Joints'}, 'description': {'en-US': 'Student has started module 1: Method of joints'}, 'type': 'http://adlnet.gov/expapi/activities/course'}, 'objectType': 'Activity'}, 'result': {'duration': 'PT2S'}, 'id': '4e66ecaa-48f6-4b14-b948-f15068983b33', 'timestamp': '2021-06-15T02:14:02.632Z', 'stored': '2021-06-15T02:14:02.632Z', 'authority': {'objectType': 'Agent', 'account': {'homePage': 'https://xcite-testing.lrs.io/keys/authorization', 'name': 'authorization'}}}
acessing key and result in statement at index 0:
https://elearn.ucr.edu/courses/3730
#lets try to get a bar chart that will do show the number of unique actors Y vs verbs X
#iterate thru data and save
actors = [] #kinda like an list ("array") of strings... idk what the data would be... list?
for eachStatement in data:
actors.append(eachStatement['actor']['name'])
##print(actors)
#print(actors[0])
verbs = []
for eachStatement in data:
verbs.append(eachStatement['verb']['display']['en-US'])
#print(verbs)
#using Basic Bar Chart with plotly.graph_objects
fig = go.Figure([go.Bar(x= verbs, y= actors)])
fig.show()
#You do see the bars, but the colors appear grayed out because each bar is composed of hundreds of stacked rectangles with a tiny space between them
#https://stackoverflow.com/questions/66177855/bar-colors-in-plotly-bar-chart-appear-grayed-out
Above graphs probably have to many entries, when need to trim it down.
# using set()
# to remove duplicated
# from list
trimmed_actors = list(set(actors))
trimmed_verbs = list(set(verbs))
fig = go.Figure([go.Bar(x= trimmed_verbs, y= trimmed_actors)])
fig.show()
Looks better but still isnt what i want..
timesVisted = [0,0,0,0]
for eachStatement in data:
for eachVerb in range(4):
#print(eachVerb) since eachVerb is a string, whenn doing for eachVerb in trimmed_Verbs it doesnt work an index
if eachStatement['verb']['display']['en-US'] == trimmed_verbs[eachVerb]:
timesVisted[eachVerb] += 1
#print(timesVisted[0])
fig = go.Figure([go.Bar(x= trimmed_verbs, y= timesVisted)])
fig.show()
Here's the data output from veracity makinf each unqiue bar for verb where the matching records is the object id
"data": [ { "_id": "answered", "count": 12971, "canonical": { "display": "answered" }, "value": 12971, "name": "answered", "_category": "answered" }, { "_id": "viewed", "count": 7339, "canonical": { "display": "viewed" }, "value": 7339, "name": "viewed", "_category": "viewed" }, { "_id": "initialized", "count": 677, "canonical": { "display": "initialized" }, "value": 677, "name": "initialized", "_category": "initialized" }, { "_id": "exited", "count": 338, "canonical": { "display": "exited" }, "value": 338, "name": "exited", "_category": "exited" } ]
plotly.offline.init_notebook_mode()